using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using log4net;
using NHibernate;
using System.ComponentModel;
using TanashCMS.Business.Task;
using TanashCMS.Business.Utilities;
using System.Xml.Serialization;

namespace TanashCMS.Business
{
    [DataObject(true)]
    public class Category : CMSDataObject
    {
        private static readonly ILog actionLog = LogManager.GetLogger(typeof(InventoryTask));
        private int mWebsiteID = 0;
        private int mParentID;
        private bool m_isDefault;
        private string mPhotoURL;
        private int mOrderIndex;
        public virtual int WebsiteID { get { return mWebsiteID; } set { mWebsiteID = value; } }
        public virtual int ParentID { get { return mParentID; } set { mParentID = value; } }
        public virtual bool IsDefault { get { return m_isDefault; } set { m_isDefault = value; } }
        public virtual string PhotoURL { get { return mPhotoURL; } set { mPhotoURL = value; } }
        public virtual int OrderIndex { get { return mOrderIndex; } set { mOrderIndex = value; } }

        public static List<Category> List(int WebsiteID, bool Recursive, string OrderBy)
        {
            string query = "select c from Category c where c.WebsiteID = " + WebsiteID.ToString();
            if (string.IsNullOrEmpty(OrderBy)) OrderBy = "OrderIndex";
            if (!Recursive)
                query += " and c.ParentID = 0";
            query += " and c.State = " + ((int)CMSObjectState.Published).ToString();
            query += " order by c." + OrderBy;
            return (List<Category>)Configuration.GetCurrentSession().CreateQuery(query).List<Category>();
        }
        public static List<Category> List(int WebsiteID, bool Recursive)
        {
            return List(WebsiteID, Recursive, null);
        }
        public static List<Category> List(int WebsiteID, int ParentID, string OrderBy)
        {
            if (string.IsNullOrEmpty(OrderBy)) OrderBy = "OrderIndex";
            return (List<Category>)Configuration.GetCurrentSession().CreateQuery("select c from Category c where c.ParentID = " + ParentID.ToString() + " and c.WebsiteID = " + WebsiteID.ToString() + " and State = " + ((int)CMSObjectState.Published).ToString() + " order by c." + OrderBy).List<Category>();
        }
        public static List<Category> List(int WebsiteID, int ParentID)
        {
            return List(WebsiteID, ParentID, null);
        }

        public static Category Get(int id)
        {
            return Configuration.GetCurrentSession().Get<Category>(id);
        }

        public static void Update(Category category)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                category.LastUpdated = DateTime.Now;


                LogAction(category, "updated", "user");

                TanashWebsite website = session.Get<TanashWebsite>(category.WebsiteID);
                category.mWebsiteID = website.ID;
                session.SaveOrUpdate(category);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Insert(Category category)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                TanashWebsite website = session.Get<TanashWebsite>(category.WebsiteID);
                category.mWebsiteID = website.ID;
                session.Save(category);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }

        public static void Delete(Category category)
        {
            ISession session = Configuration.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();

            try
            {
                session.Delete(category);
                tx.Commit();
                session.Flush();
                session.Disconnect();
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                throw ex;
            }
        }
    }
}
